import lib_car from "../../../api/lib_car"
import _activity from "../../../cache/_activity"
import _object from "../../../utils/_object";
import _series from "../../../cache/_series"
import _layer from "../../../common/_layer";
import _multiple_ui from "../../../cache/_multiple_ui";

Page({
  data: {
    curr_product: '',
    curr_id: '',
    product_list: [],
    product_id: 0,
    series: '',
    series_id: '',

    activity_id: '',
    tids: 0,
    clickid: '',
    is_multiple: false
  },
  onLoad: function (options) {
    if (_object._length(options) > 0 && options.hasOwnProperty('product_id')) {
      this.setData({
        curr_id: options.product_id
      })
    }


  },
  onShow() {

    if (_activity.get() && _object._length(_activity.get()) > 0) {
      this.setData({
        activity_id: _activity.get().activity_id,
        tids: _activity.get().tids,
        clickid: _activity.get().clickid
      })
    }

    //多品UI
    //UI
    let multiple_ui = _multiple_ui.get();

    if (multiple_ui && multiple_ui.is_multiple == 1) {
      this.setData({
        is_multiple: true,
      })
    } else {
      this.setData({
        is_multiple: false
      })
    }

    let cach_series = _series.get()
    if (cach_series && _object._length(cach_series) > 0) {
      this.setData({
        series: cach_series.series_name,
        series_id: cach_series.series_id
      })
    }

    this.init_product()

  },

  //车款列表
  init_product() {
    lib_car.productyc(this.data.series_id, this.data.series).then((res) => {
      if (res.code != 0) {
        _layer.message(res.msg);
        return;
      }
      this.setData({
        product_list: res.data
      })

    })
  },
  // 切换车款
  change_product(e) {
    const { item, index } = e.currentTarget.dataset;
    const product_name = item.year + '款 ' + item.product_name
    this.setData({ curr_id: item.id })
    //切换 传参车款
    tt.redirectTo({
      url: `/packageMy/pages/pricelist/pricelist?product_name=${product_name}&product_id=${item.id}`,
    });
  },

  //分享
  async onShareAppMessage(option) {

    const { activity_id, tids, clickid } = this.data

    //单品跟多品的分享  单品--list页面  多品-- 报名页

    if (this.data.is_multiple == true) {
      return {
        title: '查询底价',
        path: `/pages/multiple/common/common?activity_id=${activity_id}&tids=${tids}&clickid=${clickid}`, // ?后面的参数会在转发页面打开时传入onLoad方法
        imageUrl: '/assets/image/multiple_banner.jpg', // 支持本地或远程图片，默认是小程序 icon
        success(res) {
          console.log('分享成功', res);
        },
        fail() {
          console.log("转发发布器调起失败");
        },
      };

    } else {
      return {
        title: '查询底价',
        path: `/pages/single/list/list?activity_id=${activity_id}&tids=${tids}&clickid=${clickid}`, // ?后面的参数会在转发页面打开时传入onLoad方法
        imageUrl: '/assets/image/banner.png', // 支持本地或远程图片，默认是小程序 icon
        success(res) {
          console.log('分享成功', res);
        },
        fail() {
          console.log("转发发布器调起失败");
        },
      };
    }
  },

})